//
//  NoteTableViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/29.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "NoteTableViewController.h"
#import "MyNoteBookViewController.h"
#import "InfoTextAttachment.h"
#import "MyNoteSingleton.h"
@interface NoteTableViewController ()
@property(nonatomic,assign)BOOL isHave;
@end

@implementation NoteTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"日记管理";
    
    self.navigationController.navigationBar.translucent = NO;
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"weibo_backBlack@2x"]  style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemAciton:)];
    
    self.navigationItem.leftBarButtonItem = barButton;
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem.title = @"管理";
    
    [MyNoteSingleton shareInstance].reloadBlcok = ^(){
        
        self.myArray = nil;
        self.myArray = [[ACHdb sharedInstance] showTheTableAll];
        NSLog(@"%@",self.myArray.firstObject);
        [self.tableView reloadData];
        
    };
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    
    imageView.image = [UIImage imageNamed:@"cm2_blk_facemask@3x"];
    
    self.tableView.backgroundView = imageView;
    
    
}
- (void)barButtonItemAciton:(UIBarButtonItem *)button{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.myArray.firstObject count] != 0 ) {
      return [self.myArray.firstObject count];
    }
    else return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";
    
    UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = [self giveMeATitle:indexPath.row];
    cell.textLabel.font =  [UIFont fontWithName:@"Courier" size:16];
    cell.detailTextLabel.text = [self changeDateWithIndexPath:indexPath.row];
    cell.detailTextLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:13];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
 
    MyNoteBookViewController *noteBook = [[MyNoteBookViewController alloc]init];
    
    
    noteBook.diaryModel = [[ACHdb sharedInstance] showOneWithDate:self.myArray.firstObject[indexPath.row]];
    
    NSArray *array = [noteBook.diaryModel.tape componentsSeparatedByString:@"/"];
    
    __block NSArray *arr =  array;
    __block int i = 0;
    __block int j = 0;
    [noteBook.diaryModel.diary enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, noteBook.diaryModel.diary.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSTextAttachment *value, NSRange range, BOOL *stop) {
        
        if (value && [value isKindOfClass:[InfoTextAttachment class]]) {
            
            ((InfoTextAttachment*)value).infoString = arr[i];
            i++;
            
            value.bounds = CGRectMake(0, 0,kP6width(300), kP6Height(30));
            
        }
        else if(value && [value isKindOfClass:[NSTextAttachment class]]){
            
            NSData *data = noteBook.diaryModel.picArray[j];
            value.image =  [UIImage imageWithData:data scale:1.0 ];
            j++;
            CGSize size = value.image.size;
            value.bounds = CGRectMake(0, 0, kP6width(80), kP6Height(80) * size.height/ size.width );
            
        }
    }];
    
    
    
    
    
    [self presentViewController:noteBook animated:YES completion:nil];
    
    
    

    
    
    
}

- (NSString *)changeDateWithIndexPath:(NSInteger)path{
    
    NSMutableString *str = [NSMutableString stringWithFormat:@"%@",self.myArray.firstObject[path]];
    
    if (str && str.length != 0) {
        
    
    [str insertString:@"/" atIndex:4];
    [str insertString:@"/" atIndex:7];
    [str insertString:@"/" atIndex:10];
    [str insertString:@":" atIndex:13];
    [str insertString:@":" atIndex:16];
    
    return str;
    }
    else return  @"";
}

-(NSString *)giveMeATitle:(NSInteger)path{
    
    if ([self.myArray.lastObject[path] length] != 0) {
        return self.myArray.lastObject[path];
        
    }
    else{
        return @"<无标题>";
    }
    
    
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return UITableViewCellEditingStyleDelete;
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
      [[ACHdb sharedInstance] deleteWithDate:self.myArray.firstObject[indexPath.row]];
        [self.myArray.firstObject removeObjectAtIndex:indexPath.row];
        [self.myArray.lastObject removeObjectAtIndex:indexPath.row];
        
        
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
